import com.alibaba.fastjson.parser.ParserConfig
import com.alibaba.fastjson2.JSON
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.meatball.iot.common.mqtt.StrategyFactory
import com.meatball.iot.common.mqtt.enums.MqttStrangelyEnum
import com.meatball.iot.driver.doormagnet.data.DoorMagnetDto
import com.meatball.iot.driver.doormagnet.data.DuYunDto
import com.meatball.iot.driver.doormagnet.enums.Cmd
import org.junit.jupiter.api.Test

class MyTest {
    @Test
    fun parseData() {
        val data = "00 1F 01 CC 20 00 1A 02 00 00 00 00 00 00 00 00 00 40 01 00 00 00 01 01 02 93 22 00 02 01 02 00 00 00 03 01 02 00 00 00 01 02 02 63 01 00 00 02 02 02 00 00 00 00 03 02 02 00 00 00 00 00 03 02 57 02 00 00 00 06 02 48 07 58 03 00 00"
        val bytes = data.split(" ").map { it.toInt(16).toByte() }.toByteArray()
        val deviceID = bytes.sliceArray(1..4).joinToString("") { "%02X".format(it) }
        println("设备ID: ${deviceID.toInt(16)}")
        val frameCount = bytes.sliceArray(6..7).joinToString("") { "%02X".format(it) }
        println("帧计数: $frameCount")
//        val groupActiveTotalEnergy = ByteBuffer.wrap(bytes, 17, 4).order(ByteOrder.LITTLE_ENDIAN)
        val groupActiveTotalEnergy = bytes.sliceArray(17..20).reversedArray().joinToString("") { "%02X".format(it) }.toInt()
        println("组合有功总电能: ${groupActiveTotalEnergy / 100.00}")
        val aVoltage = bytes.sliceArray(25..27).reversedArray().joinToString("") { "%02X".format(it) }.toInt()
        println("A相电压: ${aVoltage / 10.0}")
        val aCurrent = bytes.sliceArray(43..45).reversedArray().joinToString("") { "%02X".format(it) }.toInt() / 1000.000
        println("A电流: ${aCurrent}A")
    }

    @Test
    fun door() {
        val a = """
        {
        	"msgType": "dataPoints",
        	"subData": {
        		"datastreamId": "3339_0_1",
        		"deviceId": "2168263848",
        		"deviceName": "MC_TEST",
        		"imei": "866808067238054",
        		"productId": "xRrv6J9xbl",
        		"time": 1717293984979,
        		"value": {
        			"data": "BgBCGyg=",
        			"type": 0
        		}
        	}
        }
    """.trimIndent()

        val b = JSON.parseObject(a, DoorMagnetDto::class.java)
        println(b)
    }

    @Test
    fun door2() {
        val a = "7b22636d64223a322c227374617465223a312c226576656e74223a322c2276626174223a332e30362c22637371223a397d"
        val b = hexToString(a)
        println(b)
    }

    private fun hexToString(hex: String): String {
        val output = StringBuilder()

        // 每两个字符一组，转换为一个字符
        for (i in hex.indices step 2) {
            val str = hex.substring(i, i + 2)
            val decimal = str.toInt(16)
            output.append(decimal.toChar())
        }

        return output.toString()
    }

    private fun String.decodeHex(): String {
        require(length % 2 == 0) { "传入的hex长度不合法" }
        return String(
            chunked(2)
                .map { it.toInt(16).toByte() }
                .toByteArray()
        )
    }

    @Test
    fun door3() {
        // 注册自定义反序列化器
//        ParserConfig.getGlobalInstance().putDeserializer(Cmd::class.java, CmdDeserializer())
        val a = "7b22636d64223a322c227374617465223a312c226576656e74223a322c2276626174223a332e30322c22637371223a387d"
        val b = a.decodeHex()
        println(b)
        val c = JSON.parseObject(b, DuYunDto::class.java)
        println(c)
    }

    @Test
    fun test() {
        val b = MqttStrangelyEnum.fromCode("smokealarm")
        val c = StrategyFactory().getMqttStrategy(b)
        c.toStart("你好", "12312313")
    }
}